package com.isoft.src.main.java.ui;

import com.isoft.src.main.java.model.Order;
import com.isoft.src.main.java.model.Product;
import com.isoft.src.main.java.model.User;
import com.isoft.src.main.java.service.ProductService;
import com.isoft.src.main.java.service.UserService;

import java.util.Map;
import java.util.Scanner;

public class UserInterface {

    private UserService userService;
    private ProductService productService;
    private User currentUser;

    public UserInterface(UserService userService, ProductService productService, User currentUser) {
        this.userService = userService;
        this.productService = productService;
        this.currentUser = currentUser;
    }

    public void showMenu() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("1. 浏览商品");
            System.out.println("2. 添加商品到购物车");
            System.out.println("3. 查看购物车"); // Added option for viewing cart
            System.out.println("4. 购买");
            System.out.println("5. 退出");

            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            switch (choice) {
                case 1:
                    browseProducts();
                    break;
                case 2:
                    addProductToCart(scanner);
                    break;
                case 3:
                    viewCart(); // Added method call
                    break;
                case 4:
                    checkout();
                    break;
                case 5:
                    return;
                default:
                    System.out.println("无效操作");
            }
        }
    }

    private void browseProducts() {
        System.out.println("可获取的商品:");
        productService.getAllProducts().values().forEach(System.out::println);
    }

    private void addProductToCart(Scanner scanner) {
        System.out.print("输入要添加到购物车的商品ID: ");
        String productId = scanner.nextLine();
        Product product = productService.getAllProducts().get(productId);

        if (product != null) {
            System.out.print("输入要添加的数量: ");
            int quantity = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            if (quantity > 0) {
                currentUser.getCart().addProduct(product, quantity);
                System.out.println("商品已添加到购物车!");
            } else {
                System.out.println("数量必须大于零.");
            }
        } else {
            System.out.println("无效的商品ID.");
        }
    }

    private void viewCart() {
        System.out.println("当前购物车内容:");
        System.out.println(currentUser.getCart()); // Print the contents of the cart
    }

    private void checkout() {
        if (currentUser.getCart().isEmpty()) {
            System.out.println("购物车为空，无法完成购买.");
            return;
        }

        Scanner scanner = new Scanner(System.in);
        System.out.print("确认购买购物车中的所有商品? (yes/no): ");
        String confirmation = scanner.nextLine();

        if (confirmation.equalsIgnoreCase("yes")) {
            Order order = new Order("order" + System.currentTimeMillis(), currentUser.getCart(), new java.util.Date());
            currentUser.checkout(order);
            System.out.println("支付成功!");
            currentUser.getCart().clear(); // Clear the cart after purchase
        } else {
            System.out.println("购买已取消.");
        }
    }
}
